package cn.legend.travel.admin.authorize.dao.persist.repository.impl;

import cn.legend.travel.admin.authorize.dao.persist.mapper.RoleBackendApiMapper;
import cn.legend.travel.admin.authorize.dao.persist.repository.IRoleBackendApiRepository;
import cn.legend.travel.admin.authorize.pojo.entity.RoleBackendApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class RoleBackendApiRepositoryImpl implements IRoleBackendApiRepository {
    private final RoleBackendApiMapper mapper;

    @Override
    public int addNewPermission(Long roleId, Long apiId) {
        RoleBackendApi roleBackendApi = new RoleBackendApi();
        roleBackendApi.setRoleId(roleId);
        roleBackendApi.setBackendApiId(apiId);
        return mapper.insert(roleBackendApi);
    }

    @Override
    public int deletePermission(Long roleId, Long apiId) {
        QueryWrapper<RoleBackendApi> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        wrapper.eq("backend_api_id", apiId);
        return mapper.delete(wrapper);
    }

    @Override
    public int countByRoleIdAndApiId(Long roleId, Long apiId) {
        QueryWrapper<RoleBackendApi> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        wrapper.eq("backend_api_id", apiId);
        return mapper.selectCount(wrapper);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        QueryWrapper<RoleBackendApi> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        mapper.delete(wrapper);
    }

    @Override
    public int countByRoleId(Long roleId) {
        QueryWrapper<RoleBackendApi> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        return mapper.selectCount(wrapper);
    }

    @Override
    public int getByRoleAndMenuId(Long roleId, Long menuId) {
        return mapper.getByRoleAndMenuId(roleId, menuId);
    }
}
